package cn.wyl.org.controller;

import cn.wyl.basic.util.AjaxResult;
import cn.wyl.basic.util.PageList;
import cn.wyl.org.domain.Employee;
import cn.wyl.org.query.EmployeeQuery;
import cn.wyl.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工相关功能的API",description = "员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    //Get http://localhost/employee/1
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    //Get http://localhost/employee
    @GetMapping
    @ApiOperation(value = "查询全部")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 分页查询
     * @param employeeQuery
     * @return
     */
    //post http://localhost/employee 对象
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    //delete http://localhost/employee/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改接口
     * @param employee
     * @return
     */
    //put http://localhost/employee {对象}
    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @GetMapping("/list/{shopId}")
    public List<Employee> findByShopId(@PathVariable("shopId")Long shopId){
        return employeeService.findByShopId(shopId);
    }
}
